package com.software.levelupandroid.loginAndRegistration;

import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.software.levelupandroid.R;
import com.software.levelupandroid.home.HomeActivity;
import com.software.levelupandroid.entity.User;
import com.software.levelupandroid.loginAndRegistration.presenter.IPresenter;
import com.software.levelupandroid.loginAndRegistration.presenter.MainPresenter;


public class LoginActivity extends AppCompatActivity implements IView{
    private Button loginButton, registerButton, forgotPasswordButton;
    private EditText nameEditText, passwordEditText;
    private MyListener myListener;

    private IPresenter mainPresenter;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mainPresenter = new MainPresenter(this,this);

        findViews();
        //进度条缓冲
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在登录...");
        progressDialog.setCancelable(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        setListener();
    }

    private void findViews() {
        loginButton = findViewById(R.id.login_login);
        registerButton = findViewById(R.id.login_regist);
        forgotPasswordButton = findViewById(R.id.login_forget_pwd);
        nameEditText = findViewById(R.id.login_name);
        passwordEditText = findViewById(R.id.login_pwd);
    }

    private void setListener() {
        myListener = new MyListener();
        loginButton.setOnClickListener(myListener);
        registerButton.setOnClickListener(myListener);
        forgotPasswordButton.setOnClickListener(myListener);

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void loginSuccess(User user) {
        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
        startActivity(intent);
    }

    @Override
    public void loginFailure(String msg) {

    }

    class MyListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.login_login:
                    //TODO 如果满足条件跳转到主界面
                    String email = nameEditText.getText().toString();
                    String password = passwordEditText.getText().toString();
                    mainPresenter.login(email,password);
//                    Intent toHome = new Intent(LoginActivity.this, HomeActivity.class);
//                    startActivity(toHome);
                    break;
                case R.id.login_regist:
                    Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                    startActivity(intent);
                    break;
                case R.id.login_forget_pwd:
                    //todo 忘记密码界面
//                    Intent intent2 = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
//                    startActivity(intent2);
                    break;
                default:
                    break;
            }
        }
    }
}